package com.ideal.proxy.impl;

import com.alibaba.fastjson.JSONObject;
import com.ideal.proxy.DocProxy;
import com.ideal.proxy.TocProxy;
import com.ideal.proxy.entity.DocRequest;
import com.ideal.proxy.entity.TocRequest;
import com.ideal.util.HttpClientUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * <h1> 语雀文档相关接口实现类 </h1>
 *
 * @author ideal-20
 * @date 2023-08-05 01:15
 **/
public class DocProxyImpl implements DocProxy {
    @Override
    public String getDocBySlug(DocRequest docRequest) {
        if (docRequest == null || StringUtils.isBlank(docRequest.getToken()) || StringUtils.isBlank(docRequest.getRepoNamespace())) {
            return null;
        }
        String url = docRequest.getApiUrl() + "/repos/" + docRequest.getRepoNamespace() + "/docs/" + docRequest.getSlug();
        Map<String, String> headers = new HashMap<>();
        headers.put("User-Agent", docRequest.getSource());
        headers.put("X-Auth-Token", docRequest.getToken());
        String result = HttpClientUtil.doGet(url, null, headers);
        try {
            return JSONObject.parseObject(result).getJSONObject("data").getString("body");
        } catch (Exception e) {
            return null;
        }
    }
}